﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Aphysoft.Vate
{
    public static class SizeHelper
    {
        private static readonly int[,] StandardWidthSize = { { 1920, 1920, 1600 }, { 1280, 1280, 1067 }, { 768, 768, 640 } };
        private static readonly int[,] StandardHeightSize = { { 1200, 1080, 1200 }, { 800, 720, 800 }, { 480, 432, 480 } };

        // standard size is 

        // FULL HD
        // - 1920 x 1200 for 16 : 10
        // - 1920 x 1080 for 16 : 9
        // - 1600 x 1200 for 4 : 3
        // HD
        // - 1280 x 800 for 16 : 10
        // - 1280 x 720 for 16 : 9
        // - 1067 x 800 for 4 : 3
        // SD
        // - 768 x 480 for 16 : 10
        // - 768 x 432 for 16 : 9
        // - 640 x 480 for 4 : 3

        // at this size, every UI are same.

        private static int StandardDefinitionIndex
        {
            get
            {
                int standardDefinitionIndex;
                switch (Vate.Definition)
                {
                    case DefinitionSize.FullHD: standardDefinitionIndex = 0; break;
                    case DefinitionSize.HD: standardDefinitionIndex = 1; break;
                    case DefinitionSize.SD: standardDefinitionIndex = 2; break;
                    default: standardDefinitionIndex = 1; break;
                }
                return standardDefinitionIndex;
            }
        }

        private static int StandardAspectRatioIndex
        {
            get
            {
                int standardAspectRatioIndex;
                switch (Vate.AspectRatio)
                {
                    case ScreenRatio.SixteenToTen: standardAspectRatioIndex = 0; break;
                    case ScreenRatio.SixteenToNine: standardAspectRatioIndex = 1; break;
                    case ScreenRatio.FourToThree: standardAspectRatioIndex = 2; break;
                    default: standardAspectRatioIndex = 1; break;
                }
                return standardAspectRatioIndex;
            }
        }

        private static int _perf_pwidth = 0;
        private static float _perf_xscale = 0;
        private static int _perf_pheight = 0;
        private static float _perf_yscale = 0;

        public static float XScale
        {
            get
            {
                int pwidth = Vate.Width;

                if (pwidth != _perf_pwidth)
                {
                    int stdwidth = StandardWidthSize[StandardDefinitionIndex, StandardAspectRatioIndex];

                    _perf_pwidth = pwidth;
                    _perf_xscale = ((float)pwidth / (float)stdwidth);

                    return _perf_xscale;
                }
                else
                    return _perf_xscale;
            }
        }
        
        public static float YScale
        {
            get
            {         
                int pheight = Vate.Height;

                if (pheight != _perf_pheight)
                {
                    int stdheight = StandardHeightSize[StandardDefinitionIndex, StandardAspectRatioIndex];

                    _perf_pheight = pheight;
                    _perf_yscale = ((float)pheight / (float)stdheight);

                    return _perf_yscale;
                }
                else
                    return _perf_yscale;
            }
        }

        public static int ScaleX(int originalX)
        {
            if (originalX == 0)
                return 0;
            else
                return (int)(originalX * XScale);
        }
        public static int ScaleY(int originalY)
        {
            if (originalY == 0)
                return 0;
            else
                return (int)(originalY * YScale);
        }
        public static int ScaleX(float originalX)
        {
            if (originalX == 0)
                return 0;
            else
                return (int)(originalX * XScale);
        }
        public static int ScaleY(float originalY)
        {
            if (originalY == 0)
                return 0;
            else
                return (int)(originalY * YScale);
        }
        public static float ScaleXFloat(int originalX)
        {
            if (originalX == 0)
                return 0;
            else
                return (originalX * XScale);
        }
        public static float ScaleYFloat(int originalY)
        {
            if (originalY == 0)
                return 0;
            else
                return (originalY * YScale);
        }
        public static Vector2 ScaleVector2(float x, float y)
        {
            return new Vector2(x * XScale, y * YScale);
        }
    }
}
